package com.tql.pandian.controller;

import com.ruoyi.common.core.utils.CalculatedValueUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.tql.pandian.entity.IncaseDto;
import com.tql.pandian.entity.ProlossDetialDto;
import com.tql.pandian.entity.TqlStapProloss;
import com.tql.pandian.entity.TqlStapProlossDiagnosisInfo;
import com.tql.pandian.service.TqlStapProlossService;
import com.tql.pandian.vo.StapProlossVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/originalStapProloss")
@Api(tags = "原材料损益分析")
public class OriginalStapProlossController  extends BaseController {

    @Autowired
    private TqlStapProlossService tqlStapProlossService;

    @GetMapping("/list")
    @ApiOperation("原材料损益")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pandianCode", value  = "pandianCode", paramType = "query", dataType = "string")
    })

    public AjaxResult stapProlossAnalys(@ApiIgnore @RequestParam Map<String, Object> params) {
        Object pandianCode=params.get("pandianCode");
        List<TqlStapProlossDiagnosisInfo> data = tqlStapProlossService.stapProlossAnalys((String)pandianCode);
        return success(data);
    }

    @GetMapping("/detail")
    @ApiOperation("原材料损益详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value  = "ids", paramType = "query", dataType = "string")
    })

    public AjaxResult stapProlossAnalysDetail(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<TqlStapProloss> data =null;
        Object ids=params.get("ids");
        if(null!=ids)
        {
            String s=(String)ids;
            String[] idArr= StringUtils.split(s,",");
            Long[] longArr = Arrays.stream(idArr).map(Long::parseLong).toArray(Long[]::new);
            data = tqlStapProlossService.stapProlossAnalysDetail(longArr);
        }

        return success(data);
    }

    @GetMapping("/stapDetail/{id}")
    @ApiOperation("原材料损益物料详情")
    public AjaxResult stapDetail(@PathVariable("id") Long id) {
        TqlStapProloss dto=tqlStapProlossService.findStapProloss(id);
        ProlossDetialDto data = tqlStapProlossService.findStapProlossDetailByPid(id);
        StapProlossVo vo=new StapProlossVo();
        vo.setMain(dto);
        vo.setDetail(data);
        return success(vo);
    }


    @GetMapping("/detaiInfo/{id}/{month}")
    @ApiOperation("原材料损益物料详情")
    public AjaxResult detaiInfo(@PathVariable("id") Long id,@PathVariable("month") Integer month) {
        //TqlStapProloss dto=tqlStapProlossService.findStapProloss(id);
        ProlossDetialDto data = tqlStapProlossService.findStapProlossDetailByPid(id);
        String incaseIds=data.getIncaseIds();
        String transferOutIds=data.getTransferOutIds();
        String transferInIds=data.getTransferInIds();
        String purcharseIds=data.getPurcharseIds();
        Long thisPandianId=data.getThisPandianId();
        Long lastPandianId=data.getLastPandianId();
        List<IncaseDto> list1=null;
        List<IncaseDto> list2=null;
        List<IncaseDto> list3=null;
        List<IncaseDto> list4=null;
        ArrayList<IncaseDto> arrayList=new ArrayList<>();
        if(!Objects.isNull(lastPandianId))
        {
            IncaseDto last=tqlStapProlossService.findPandianById(lastPandianId);
            if(null!=last)
            {
                BigDecimal bg=null==last.getFRealQty()?BigDecimal.ZERO:last.getFRealQty();
                BigDecimal price=null==last.getFPRICE()?BigDecimal.ZERO:last.getFPRICE();
                BigDecimal d=CalculatedValueUtil.multiplyB(bg,price);
                last.setFAmount(d);
                arrayList.add(last);
            }

        }

        if(!StringUtils.isEmpty(incaseIds))
        {
            list1=tqlStapProlossService.findInfo(incaseIds,month,1);
            if(!CollectionUtils.isEmpty(list1))
            arrayList.addAll(list1);
        }
        if(!StringUtils.isEmpty(transferOutIds))
        {
            list2=tqlStapProlossService.findInfo(transferOutIds,month,2);
            if(!CollectionUtils.isEmpty(list2))
                arrayList.addAll(list2);
        }
        if(!StringUtils.isEmpty(transferInIds))
        {
            list3=tqlStapProlossService.findInfo(transferInIds,month,3);
            if(!CollectionUtils.isEmpty(list3))
                arrayList.addAll(list3);
        }
        if(!StringUtils.isEmpty(purcharseIds))
        {
            list4=tqlStapProlossService.findInfo(purcharseIds,month,4);
            if(!CollectionUtils.isEmpty(list4))
                arrayList.addAll(list4);
        }



        if(!Objects.isNull(thisPandianId))
        {
            IncaseDto thisl=tqlStapProlossService.findPandianById(thisPandianId);
            if(null!=thisl)
            {
                BigDecimal bg=null==thisl.getFRealQty()?BigDecimal.ZERO:thisl.getFRealQty();
                BigDecimal price=null==thisl.getFPRICE()?BigDecimal.ZERO:thisl.getFPRICE();
                BigDecimal d=CalculatedValueUtil.multiplyB(bg,price);
                thisl.setFAmount(d);
                arrayList.add(thisl);
            }
        }


        return success(arrayList);
    }

}
